#include "bitmap.h"

// create new bit map and reset
struct bitmap_t *
create_bitmap()
{
  struct bitmap_t *ret;

  ret = (struct bitmap_t *)malloc(sizeof(struct bitmap_t));
  memset(ret, 0, sizeof(struct bitmap_t));

  return ret;
}

// set the position in bitmap
void set_bitmap(struct bitmap_t *map, uint32_t pos)
{
  // set position;
  map->data |= (1 << pos);
  map->len++;
}

// reset the position in bitmap
void reset_bitmap(struct bitmap_t *map, uint32_t pos)
{
  uint32_t mask = ~(1 << pos);
  map->data &= mask;
  map->len--;
}

// return true if bitmap[pos] is set else false
bool is_bitmap_set(struct bitmap_t *map, uint32_t pos)
{
  return (map->data >> pos) & 1;
}

// print it out
void print_bitmap(struct bitmap_t *map)
{
  for (int i = 0; i < 20; i++)
  {
    if (is_bitmap_set(map, i))
      printf("1");
    else
      printf("0");
  }

  printf("\n");
}
